package com.example.its2021606134chh.MyMessageModule;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MM_statisticalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MM_statisticalFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private PieChart piechart;
    ArrayList<PieEntry> entries;
    PieData pieData;
    PieDataSet dataSet;
    List<MessageParams> listTemperature = new ArrayList<MessageParams>();
    List<MessageParams> listHumidity = new ArrayList<MessageParams>();
    List<MessageParams> listLightIntensity = new ArrayList<MessageParams>();
    List<MessageParams> listCo2 = new ArrayList<MessageParams>();
    List<MessageParams> listPm25 = new ArrayList<MessageParams>();

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MM_statisticalFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MM_statisticalFragment newInstance(String param1, String param2) {
        MM_statisticalFragment fragment = new MM_statisticalFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_m_m_statistical, container, false);
        piechart = (PieChart) view.findViewById(R.id.piechart);
        initPieChart();
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {

                    }

                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        ThresholdParams params = gson.fromJson(json, ThresholdParams.class);
                        int Temperature = Integer.parseInt(params.getTemperature());
                        int Humidity = Integer.parseInt(params.getHumidity());
                        int LightIntensity = Integer.parseInt(params.getLightIntensity());
                        int Co2 = Integer.parseInt(params.getCo2());
                        int Pm25 = Integer.parseInt(params.getPm25());
                        if (Temperature > 20) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【温度】报警");
                            messageParams.setThreshold("20");
                            messageParams.setValues(String.valueOf(Temperature));
                            listTemperature.add(messageParams);
                        }
                        if (Humidity > 80) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【湿度】报警");
                            messageParams.setThreshold("80");
                            messageParams.setValues(String.valueOf(Humidity));
                            listHumidity.add(messageParams);
                        }

                        if (LightIntensity > 2000) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【光照】报警");
                            messageParams.setThreshold("2000");
                            messageParams.setValues(String.valueOf(LightIntensity));
                            listLightIntensity.add(messageParams);
                        }
                        if (Co2 > 3000) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【Co2】报警");
                            messageParams.setThreshold("3000");
                            messageParams.setValues(String.valueOf(Co2));
                            listCo2.add(messageParams);
                        }
                        if (Pm25 > 80) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【PM2.5】报警");
                            messageParams.setThreshold("80");
                            messageParams.setValues(String.valueOf(Pm25));
                            listPm25.add(messageParams);
                        }
                        if (listPm25.size() > 0) {
                            entries.get(0).setY(listPm25.size());
                            entries.get(0).setLabel("PM2.5 " + listPm25.size());
                        } else {
                            entries.get(0).setLabel("PM2.5" + "当前还未有报警信息");
                        }
                        if (listTemperature.size() > 0) {
                            entries.get(1).setY(listTemperature.size());
                            entries.get(1).setLabel("温度" + listTemperature.size());
                        } else {
                            entries.get(1).setLabel("温度" + "当前还未有报警信息");
                        }
                        if (listHumidity.size() > 0) {
                            entries.get(2).setY(listHumidity.size());
                            entries.get(2).setLabel("湿度" + listHumidity.size());
                        } else {
                            entries.get(2).setLabel("湿度" + "当前还未有报警信息");
                        }
                        if (listLightIntensity.size() > 0) {
                            entries.get(3).setY(listLightIntensity.size());
                            entries.get(3).setLabel("光照" + listLightIntensity.size());
                        } else {
                            entries.get(3).setLabel("光照" + "当前还未有报警信息");
                        }
                        if (listCo2.size() > 0) {
                            entries.get(4).setY(listCo2.size());
                            entries.get(4).setLabel("Co2 " + listCo2.size());
                        } else {
                            entries.get(4).setLabel("Co2 " + "当前还未有报警信息");
                        }
                        piechart.notifyDataSetChanged();
                        piechart.invalidate();
                    }
                }, "get_all_sense", new String[]{"UserName"}, "user1");
            }
        };
        timer.schedule(timerTask, 0, 3000);
        // Inflate the layout for this fragment
        return view;
    }
    public void initPieChart() {
        entries = new ArrayList<>();
        entries.add(new PieEntry(0, "PM2.5"));
        entries.add(new PieEntry(0, "温度"));
        entries.add(new PieEntry(0, "湿度"));
        entries.add(new PieEntry(0, "光照"));
        entries.add(new PieEntry(0, "Co2"));
        dataSet = new PieDataSet(entries, "实时数据分析");
        List<Integer> pieColor = new ArrayList<>(); //color
        pieColor.add(Color.parseColor("#BFDD7B"));
        pieColor.add(Color.parseColor("#E3DCA1"));
        pieColor.add(Color.parseColor("#78EA5A"));
        pieColor.add(Color.parseColor("#EE80B6"));
        pieColor.add(Color.parseColor("#FF23B4"));
        dataSet.setColors(pieColor);
        dataSet.setSliceSpace(5);   //间隔
        dataSet.setDrawValues(false);   //数值
        pieData = new PieData(dataSet);
        piechart.setData(pieData);
        piechart.setDrawEntryLabels(false); //文字
        piechart.setHoleRadius(0);  //r
        piechart.setTransparentCircleRadius(0); //透明r
        Legend legend = piechart.getLegend();
        legend.setEnabled(true);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        legend.setXOffset(90);
        legend.setFormSize(10);
        legend.setTextSize(5f);
    }

}